package gbench.appdemo.ggplot.junit;

import org.junit.jupiter.api.Test;

import gbench.common.fs.FileSystem;
import gbench.common.matlib.MatlibCanvas.NVec;
import gbench.common.matlib.data.DataReader;
import gbench.common.matlib.data.DataReader.DFrame;
import gbench.common.matlib.img.ImageOps;
import gbench.appdemo.ggplot.plot.panel.PanelFrame.PlotMode;

import static gbench.appdemo.ggplot.layer.geom.GeomPoint.geom_point;
import static gbench.appdemo.ggplot.layer.geom.GeomText.geom_text;
import static gbench.appdemo.ggplot.plot.GGplot.ggplot;
import static gbench.appdemo.ggplot.aes.Aesthetics.*;
import static gbench.common.matlib.MatlibCanvas.*;
import static gbench.common.matlib.MatlibCanvas.Range.rG;
import static gbench.common.matlib.MatlibCanvas.Range.RG;
import static gbench.common.matlib.MatlibCanvas.Range.rg;
import static gbench.common.tree.LittleTree.IRecord.*;

/**
 * 
 * @author gbench
 *
 */
public class JunitGeomIris {

    @Test
    public void foo() throws InterruptedException {
        final var datafile = FileSystem.path("data.txt", this.getClass());// 数据文件
        final var iris = DataReader.loaddfs(datafile).get("iris"); // 鸢尾花
        final var df = iris.dataframe2(e -> e.derive(5, iris.fget2(5))).filter(rG(1, 5));// 去掉Id列,即第0列
        final var length = 800; // 画板尺寸
        final var size = df.size(); // 获取列数量
        final var plot = ggplot(); // 绘图对象
        
        Range.cph(rg(0, size), RG(size, 0)).map(NVec::intA).forEach(idx -> {
            plot.add(geom_point(aes("x", df.get(idx[0]), 
                "y", df.get(idx[1]), "color", "448", "size", 0.1d,"border",0.01)));
            
            if (idx[0] == idx[1]) { // 在对角线上写数字
                final var data = new DFrame("x", 100, 
                    "y", Range.rG(0,2).map(e->e*100+length/2d),
                    "text", A(df.name(idx[0]), "鸢尾花数据", NVec.of(idx)+"" ));// 文字序列
                plot.add(geom_text(data, aes("color", "f44", "fontsize", 20)));
            }// if
            
            plot.addPanel(length, length); // 增加绘图面板
        });

        // 保存图片
        ImageOps.jpgwrite(plot.print(PlotMode.GRID), "C:/Users/gbench/Desktop/tmp/iris.jpg");
        Thread.sleep(100000);
    }

}
